package com.ddone.util.mock;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author ddone
 * @date 2024/5/15-09:45
 */
public class DistanceUtil {
    private static final double EARTH_RADIUS = 6378.137D;

    public static BigDecimal getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        double lat1 = Math.toRadians(latitude1);
        double lat2 = Math.toRadians(latitude2);
        double lng1 = Math.toRadians(longitude1);
        double lng2 = Math.toRadians(longitude2);
        double a = lat1 - lat2;
        double b = lng1 - lng2;
        double s = 2.0D * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2.0D), 2.0D) +
                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2.0D), 2.0D)));
        s *= 6378.137D;
        return BigDecimal.valueOf(s).setScale(2, RoundingMode.HALF_UP);
    }

    public static void main(String[] args) {
        BigDecimal d = getDistance(116.308479D, 39.983171D, 121.48941D, 31.40527D);
        System.out.println(d);
    }
}

